package com.pingan.glue.gluegame.job;

import com.pingan.glue.gluegame.common.Constant;
import com.pingan.glue.gluegame.controller.form.FilmResponse;
import com.pingan.glue.gluegame.service.GameOrderService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.SmartLifecycle;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

@Component
public class StartFileUpdateCycle implements SmartLifecycle {

    private final static Logger LOGGER = LoggerFactory.getLogger(StartFileUpdateCycle.class);

    private volatile boolean isRunning = false;

    @Resource
    private GameOrderService gameOrderService;

    @Override
    public void start() {
        if (!isRunning) {
            new Thread(() -> {
                LOGGER.info("项目启动更新电影信息 start");
                List<FilmResponse> list = gameOrderService.film();
                Constant.FILM_RESPONSE_CATCH.clear();
                Constant.FILM_RESPONSE_CATCH.addAll(list);
                LOGGER.info("项目启动更新电影信息 end");
            }).start();
        }
        isRunning = true;
    }

    @Override
    public void stop() {
        isRunning = false;
    }

    @Override
    public boolean isRunning() {
        return false;
    }
}
